package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IOnChange{

    private RecyclerView recyclerView;
    private Button add;
    private Button searchBut;
    private EditText searchEdit;
    private List<Student> studentList = new ArrayList<>();
    private List<Student> studentArrayList = new ArrayList<>();
    private StuAdapter adapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudent();

        recyclerView = findViewById(R.id.recView);
        add = findViewById(R.id.add);
        searchBut = findViewById(R.id.seatchBut);
        searchEdit = findViewById(R.id.searchEdit);

        adapter = new StuAdapter(studentList, MainActivity.this, MainActivity.this);
        recyclerView.setAdapter(adapter);

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(manager);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });

        searchBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchList();
            }
        });

        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                searchList();
            }
        });

    }

    private void searchList() {
        String edit = searchEdit.getText().toString();
        studentArrayList = new ArrayList<>();
        for (Student  students: studentList){
            if(students.getName().contains(edit)){
                studentArrayList.add(students);
            }
        }
        adapter.setStudentList(studentArrayList);
        adapter.notifyDataSetChanged();
    }

    /**
     * 初始化学生列表
     */
    private void initStudent() {
        Student st1 = new Student("张一",18);
        Student st2 = new Student("张二",19);
        Student st3 = new Student("张三",20);
        Student st4 = new Student("张四",21);
        Student st5 = new Student("张五",22);

        studentList.add(st1);
        studentList.add(st2);
        studentList.add(st3);
        studentList.add(st4);
        studentList.add(st5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == 2001){
            Student student = new Student(data.getStringExtra("NAME"),data.getIntExtra("AGE",0));
            studentList.add(student);
            adapter.notifyDataSetChanged();
        }else if(resultCode == 2002){
            Student student = new Student(data.getStringExtra("NAME"),data.getIntExtra("AGE",0));
            studentList.get(data.getIntExtra("INDEX",0)).setName(data.getStringExtra("NAME"));
            studentList.get(data.getIntExtra("INDEX",0)).setAge(data.getIntExtra("AGE",0));
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 删除
     */
    @Override
    public void del(int i) {
        studentList.remove(i);
        adapter.notifyDataSetChanged();
    }

    /**
     * 修改
     */
    @Override
    public void edit(int i) {
        Student student = studentList.get(i);
        Toast.makeText(MainActivity.this,student.getName(),Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,EditActivity.class);
        intent.putExtra("NAME",student.getName());
        intent.putExtra("AGE",student.getAge());
        intent.putExtra("INDEX",i);
        startActivityForResult(intent,1002);
    }

}
